<template>
	<view class="head">
		<image src="../../static/logo/bg.png" class="bg-img"></image>
	</view>
	<view class="card" v-for="(item,index) in history" v-if="issearch">
		<uni-card class="card_i" :title="item.username" :extra="item.time" note="Tips">
			<view class="text">
				<view>
					识别结果:<span style="font-size: 25px;color:#66b0fc;font-weight: bold;">{{item.ans}}</span>
				</view>
			</view>
			<view class="card_img">
				<image :src="base64ToBlobUrl(item.img)" class="card_img" mode="widthFix"></image>
			</view>
		</uni-card>
	</view>
	<view class="card" v-for="(item,index) in search_h" v-else>
		<uni-card class="card_i" :title="item.username" :extra="item.time" note="Tips">
			<view class="text">
				<view>
					识别结果:<span style="font-size: 25px;color:#66b0fc;font-weight: bold;">{{item.ans}}</span>
				</view>
			</view>
			<view class="card_img">
				<image :src="base64ToBlobUrl(item.img)" class="card_img" mode="widthFix"></image>
			</view>
		</uni-card>
	</view>
</template>

<script>
	export default {
		onLoad() {
			this.get_history()

			if (uni.getStorageSync('param') !== null) {
				this.issearch = true
				this.search()
				uni.removeStorageSync('param')
			} else {
				this.issearch = false
			}
		},
		data() {
			return {
				history: [],
				search_h: [],
				issearch: false
			};
		},
		methods: {
			get_history() {
				uni.request({
					url: "http://localhost:8000/sign/identify/users",
					method: 'GET',
					data: {
						token: uni.getStorageSync('token')
					},
					success: (res) => {
						if (res.data.code === 200) {
							this.history = res.data.history
							this.history.reverse()
						}
					}
				})
			},
			base64ToBlobUrl(base64String, mimeType = "application/octet-stream") {
				// 将 Base64 字符串解码为二进制数据
				const binaryData = Uint8Array.from(atob(base64String), c => c.charCodeAt(0));

				// 创建 Blob 对象
				const blob = new Blob([binaryData], {
					type: mimeType
				});

				// 通过 URL.createObjectURL 创建 Blob 的 URL
				const blobUrl = URL.createObjectURL(blob);

				return blobUrl;
			},
			search() {
				let str = uni.getStorageSync('param')
				for (var i = 0; i < this.history.length; i++) {
					if (this.history[i].ans.includes(str)) {
						this.search_h.push(this.history[i])
					}
				}
			}
		}

	}
</script>

<style lang="scss">
	.head {
		width: 100%;
		height: 200px;

		.bg-img {
			width: 100%;
			height: 200px;
		}
	}



	.text {
		width: 250px;
		font-size: 13px;
		display: flex;
		flex-direction: column;
		justify-content: center;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		-o-text-overflow: ellipsis;
	}

	.card_img {
		width: 80px;
		height: 80px;
	}

	::v-deep .uni-card__content {
		display: flex;
		justify-content: space-between;
	}
</style>